<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class FriendController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * 显示关注者的详情页面
     */
    public function getUserinfo(Request $request,$id)
    {
        //获取当前登录用户的id
        $from = session('id');
        $fans = DB::table('fans')->where('from_id',$from)->where('by_id',$id)->first();
        //获取被关注者的详细信息
        $users = DB::table('users')->where('id',$id)->first();
        $userinfo = DB::table('user_detail')->where('id',$id)->first();
        $addres = DB::table('address')->where('uid',$id)->first();
        return view('homes.friendwb',['users'=>$users,'userinfo'=>$userinfo,'addres'=>$addres,'fans'=>$fans['status']]);
    }
    /**
     * Display a listing of the resource.
     *
     * 执行关注的方法
     */
    public function getUseradd($id,$status=0)
    {   
        $from = session('id');
        if($status==0) {
            DB::table('fans')->insert(
                ['from_id'=>$from,'by_id'=>$id,'status'=>1]
            ); 
            return back(); 
        } else {
            DB::table('fans')->where('from_id',$from)->where('by_id',$id)->delete();
            return back();
        }
    }
     /**
     * 获取我关注的用户
     */
    public function getFromuser(Request $request,$id)
    {
        $res = DB::table('fans')->where('from_id',$id)->paginate(3);
        //定义我关注的用户数
        $num = DB::table('fans')->where('from_id',$id)->count();
        //定义我的粉丝数
        $byUser = DB::table('fans')->where('by_id',$id)->count();
        //获取我的个人信息
        $myinfo = DB::table('user_detail')->where('id',$id)->first();
        $user = [];
        $count = 0;
        foreach($res as $v) {
            $user[] = array('0'=>DB::table('user_detail')->where('id',$v['by_id'])->first(),'1'=>$v);
            $count++;
        }
        return view('homes.friend',['fans'=>$user,'num'=>$num,'bycount'=>$byUser,'myinfo'=>$myinfo,'res'=>$res,'count'=>$count]);
    }
    /**
     * 获取我的粉丝
     */
    public function getByuser($id)
    {
        $res = DB::table('fans')->where('by_id',$id)->paginate(3);
        //定义我关注的用户数
        $num = DB::table('fans')->where('from_id',$id)->count();
        //定义我的粉丝数
        $byUser = DB::table('fans')->where('by_id',$id)->count();
        //获取我的个人信息
        $myinfo = DB::table('user_detail')->where('id',$id)->first();
        $user = [];
        $count = 0;
        foreach($res as $v) {
            $user[] = array('0'=>DB::table('user_detail')->where('id',$v['from_id'])->first(),'1'=>$v);
            $count++;
        }
        return view('homes.focusonyou',['fans'=>$user,'num'=>$num,'bycount'=>$byUser,'myinfo'=>$myinfo,'res'=>$res,'count'=>$count]);        
    }

}
